﻿// -----------------------------------------------------------------------
// <copyright file="LivePreviewControl.cs" company="FCIS">
// You are welcome to do whatever you want with the code in this file
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.WindowsForms
{
    using System.ComponentModel;
    using System.Windows.Forms;

    /// <summary>
    /// Live preview control, Label, TrackBar, Value
    /// </summary>
    public partial class LivePreviewControl : UserControl, INotifyPropertyChanged
    {
        /// <summary>
        /// Live preview info object
        /// </summary>
        private readonly LivePreviewInfo livePreviewInfo;

        /// <summary>
        /// Initializes a new instance of the <see cref="LivePreviewControl"/> class.
        /// </summary>
        /// <param name="livePreviewInfo">The live preview info.</param>
        public LivePreviewControl(LivePreviewInfo livePreviewInfo)
        {
            this.InitializeComponent();

            this.livePreviewInfo = livePreviewInfo;

            this.trackBar1.Value = livePreviewInfo.Value;
            this.lblValue1Value.Text = livePreviewInfo.Value.ToString();
            this.lblValue1.Text = livePreviewInfo.Label;
            this.trackBar1.Maximum = livePreviewInfo.MaxValue;
            this.trackBar1.Minimum = livePreviewInfo.MinValue;
        }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets the value.
        /// </summary>
        public int Value
        {
            get
            {
                return this.trackBar1.Value;
            }
        }

        /// <summary>
        /// Gets the info.
        /// </summary>
        public LivePreviewInfo Info
        {
            get
            {
                return this.livePreviewInfo;
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="name">The name.</param>
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        /// <summary>
        /// Tracks the bar1 scroll.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void TrackBar1Scroll(object sender, System.EventArgs e)
        {
            this.OnPropertyChanged(this.livePreviewInfo.Label);
            this.lblValue1Value.Text = this.trackBar1.Value.ToString();
        }
    }
}
